﻿
Partial Class store_sermon
    Inherits CProductPage


#Region "Menu Locations"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SetPanel1 As System.Web.UI.Control
        Get
            Return plhSetMenu
        End Get
    End Property
    Protected Overrides ReadOnly Property SetPanel2 As System.Web.UI.Control
        Get
            Return plhSetMenu2
        End Get
    End Property
    Protected Overrides ReadOnly Property BottomLinks As System.Web.UI.HtmlControls.HtmlTableRow
        Get
            Return rowLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property BottomRandom As System.Web.UI.HtmlControls.HtmlTableRow
        Get
            Return rowRandom
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'deleted
        If IsNothing(Product) Then
            Response.RedirectPermanent(CSitemap.Sermons)
            Exit Sub
        End If

        'inactive
        If Not Product.ProductIsActive Then
            Response.Redirect(CSitemap.Sermons)
            Exit Sub
        End If

        If Product.ProductTypeId_ = EProductType.Paperback Then
            Response.RedirectPermanent(CSitemap.Paperbacks)
        End If

        'old url
        With Request.FilePath.ToLower
            If .Contains("sunday_sermons/") Then Response.RedirectPermanent(CSitemap.Decide(Product))
        End With

        'Name doesnt match
        If ProductName <> HttpUtility.UrlDecode(CConfig.SafeFriendlyName(Product.ProductName)) AndAlso Request.Url.Host <> "localhost" Then
            Response.RedirectPermanent(CSitemap.Decide(Product))
            Exit Sub
        End If


    End Sub
    Protected Overrides Sub PagePreRender()
        plhSpacing.Visible = Product.HasParent AndAlso Product.BelongsTo.Children.Active.Count < 10

        'Sermon navigation: all sermons
        Dim sermons As CProductList = CProduct.Cache.Active.Sermons.SortByCreatedDescName
        NavLinks(lblNavAllSermons, sermons, Product, "All Sermons", CSitemap.Sermons)
        If Product.HasParent Then secondNavMenu.Style.Add("opacity", "0.7")

        With Product
            'Sermon navigation: by speaker
            NavLinks(lblNavSpeaker, sermons.GetBySpeaker1or2Id(.ProductSpeakerId), Product, .SpeakerName, CSitemap.Sermons(Nothing, .ProductSpeakerId))

            'Sermon navigation: by language
            If .IsEnglish Then
                plhLang.Visible = False
            Else
                NavLinks(lblNavLanguage, sermons.GetByLanguageId(.ProductLanguageId), Product, .Language.NativeAndName, CSitemap.Sermons(Nothing, 0, .ProductLanguageId))
            End If


            ctrlFader.Display(.DuncanPhotos)
            ctrlDescriptionLinks.Display(Product)

            If .HasParent Then
                lnkParent.Text = .BelongsToName 'String.Concat(.BelongsToName, " (", 1 + .BelongsTo.Children.IndexOf(Product), " of ", .BelongsTo.Children.Count, ")")
                lnkParent.NavigateUrl = CSitemap.Decide(.BelongsTo)
                If Not .IsEnglish AndAlso Not String.IsNullOrEmpty(.BelongsToNameAlt) Then
                    lnkParentAlt.Text = .BelongsToNameAlt 'String.Concat(.BelongsToNameAlt, " (", 1 + .BelongsTo.Children.IndexOf(Product), " of ", .BelongsTo.Children.Count, ")")
                    lnkParentAlt.NavigateUrl = lnkParent.NavigateUrl
                Else
                    lnkParentAlt.Visible = False
                End If
            End If

            'Metadata
            Title = String.Concat(.ProductName, " (", .SpeakerNames, ") ", .DateString)
            MetaKeywords = .ProductKeywordsOrParent
            MetaDescription = .ProductDescriptionOrParent
            If .ProductDescriptionOrParent.Length = 0 Then
                MetaDescription = String.Concat(.ProductNameAndContents, vbCrLf, .SpeakerName)
            End If
            If Not .IsEnglish Then
                Page.Title = String.Concat(.ProductName, " ", .ProductNameChineseSimplified)
                Page.MetaDescription = String.Concat(Page.MetaDescription, vbCrLf, .ProductDescriptionChineseSimplifiedOrParent)
                Page.MetaKeywords = String.Concat(Page.MetaKeywords, vbCrLf, .ProductKeywordsChineseSimplifiedOrParent)
            End If

            'Set Image
            If .HasImage Then
                imgProduct.ImageUrl = CSitemap.ProductImage(Product)
                'Paperback image is better
                If Not IsNothing(Product.CSPaperback) Then
                    imgProduct.ImageUrl = CSitemap.ProductImage(.CSPaperback)
                End If
            Else
                Dim imgUrl As String = .Speaker.RandomPhotoUrl(CSitemap.SpeakerUploads & .Speaker.SpeakerImage, .ProductId)
                If imgUrl <> CSitemap.SpeakerUploads Then 'Has speaker image
                    imgProduct.ImageUrl = imgUrl
                    imgProduct.AlternateText = .ProductName
                    imgProduct.ToolTip = .ProductName
                Else
                    imgProduct.Visible = False
                End If
            End If
            If imgProduct.Visible Then imgFb.Attributes("href") = "http://mikeconnellministries.com" & imgProduct.ImageUrl.Substring(1)


            'Title/Desc
            litTitle.Text = HttpUtility.HtmlEncode(.ProductName)
            If Not .IsEnglish Then
                litNameChineseSimplified.Text = .ProductNameChineseSimplified
            Else
                litNameChineseSimplified.Visible = False
            End If
            lblDescription.Text = .Description_Br
            If String.IsNullOrEmpty(lblDescription.Text) AndAlso Not String.IsNullOrEmpty(.Speaker.SpeakerAbout) AndAlso .ProductSpeakerId <> ESpeaker.MikeConnell Then lblDescription.Text = .Speaker.SpeakerAbout.Replace(vbCrLf, "<br/>")

            If Not .IsEnglish AndAlso Not String.IsNullOrEmpty(.ProductDescriptionChineseSimplified) Then
                lblDescriptionAlt.Text = .DescriptionChineseSimp_Br
            Else
                lblDescriptionAlt.Visible = False
            End If
            plhDescription.Visible = Not String.IsNullOrEmpty(lblDescription.Text) OrElse Not String.IsNullOrEmpty(lblDescriptionAlt.Text)


            'Keywords
            litKeywords.Text = .ProductKeywordsOrParent
            litKeywordsAlt.Text = .ProductKeywordsChineseSimplifiedOrParent
            divKeywords.Visible = .ProductKeywordsOrParent.Trim.Length > 0 OrElse Not String.IsNullOrEmpty(.ProductKeywordsChineseSimplifiedOrParent)

            If .ProductLocationId <> ELocation.Baycity Then litLocation.Text = .LocationName
            litDate.Text = .DateString
            If Not .IsEnglish Then
                litLocation.Text = String.Concat("(", .LanguageName, "/English) ", .LocationName)
            End If


            'Iframe
            With .Files
                If .HasHtm Then
                    plhNotes.Visible = True
                    iframe.Visible = True
                    iframe.Attributes.Add("src", CConfig.DropboxUrl(.FirstHtm.FileName))
                    ctrlLinks.Display(Product)
                ElseIf .HasPdfOrWord Then
                    plhNotes.Visible = True
                    iframe.Visible = True
                    iframe.Attributes.Add("src", CConfig.DropboxUrl(.FirstPdfOrWord.FileName))
                End If
            End With

            ''Link Image to Colleen's Website
            'If .ProductImageScribeId > 0 Then
            '    imgProduct.ToolTip = "Image supplied by " & .ImageScribeName
            '    If .ImageScribeUrl.Length > 0 Then
            '        CWeb.OnClick(imgProduct, "window.open('" & .ImageScribeUrl & "')")
            '    End If
            'End If

            'Speaker Links (below image)
            'lnkSpeaker.Text = CUtilities.NameAndCount(.SpeakerName, .Speaker.Products.Active.Sermons, "sermon")
            'lnkSpeaker.NavigateUrl = CSitemap.Sermons(Nothing, .ProductSpeakerId)
            'lnkSets.Visible = .Speaker.Products.Active.ConferenceSets.Count > 0
            'lnkSets.Text = CUtilities.NameAndCount(.SpeakerName, .Speaker.Products.Active.ConferenceSets, "set")
            'lnkSets.NavigateUrl = CSitemap.ConferenceSets(Nothing, .ProductSpeakerId)
            'lnkLanguage.Visible = Not .IsEnglish
            'lnkLanguage.Text = CUtilities.NameAndCount(.Language.NativeAndName, CProduct.Cache.Active.Sermons.GetByLanguageId(.ProductLanguageId), "sermon")
            'lnkLanguage.NavigateUrl = CSitemap.Sermons(Nothing, 0, .ProductLanguageId)
            If Not String.IsNullOrEmpty(.Speaker.SpeakerWebsite) Then
                lnkWebsite.Visible = True
                lnkWebsite.Text = .Speaker.SpeakerWebsite
                lnkWebsite.NavigateUrl = "http://" & lnkWebsite.Text
            End If
            If Not String.IsNullOrEmpty(.Speaker.SpeakerContactPhone) Then
                lnkOnAmazon.Visible = True
                lnkOnAmazon.Text = .SpeakerName & " on Amazon..."
                lnkOnAmazon.NavigateUrl = .Speaker.SpeakerContactPhone 'Now used for amazon link
            End If
            If .ProductSpeaker2Id > 0 Then
                lnkWebsite.Visible = True
                lnkWebsite.Text = CUtilities.NameAndCount(.Speaker2Name, .Speaker2.Products.Active, "sermon")
                lnkWebsite.NavigateUrl = CSitemap.Sermons(Nothing, .ProductSpeaker2Id)
                lnkWebsite.Target = ""
            End If


            'Facebook config
            divLike.Attributes("data-title") = .ProductName
            divLike.Attributes("data-href") = CConfig.WebsiteUrl_Product(Product)
            If .HasParent Then
                divLike.Attributes("data-href") = CConfig.WebsiteUrl_Product(.BelongsTo)
                divLike.Attributes("data-title") = .BelongsToName
            End If

            If .HasImage Then
                imgProduct.AlternateText = .ProductName
                imgProduct.ToolTip = .ProductName
                imgProduct.ImageUrl = CSitemap.ProductImage(Product)
                imgFb.Attributes("href") = "http://mikeconnellministries.com" & CSitemap.ProductThumb(Product).Substring(1)
            End If

            If .ProductVimeoId > 0 Then
                vimeo.Visible = True
                vimeo.Attributes("src") &= .ProductVimeoId
                vidSrc.Visible = True
                vidSrc.Attributes("href") = .VideoUrl_Vimeo
            ElseIf Not String.IsNullOrEmpty(.ProductYouTubeId) Then
                vimeo.Visible = True
                vimeo.Attributes("src") = "http://www.youtube.com/embed/" & .ProductYouTubeId
                vimeo.Attributes("height") = 563
                vidSrc.Visible = True
                vidSrc.Attributes("href") = .VideoUrl_YouTube 'metadata
            End If


            'Transcript
            plhTranscript.Visible = .HasTranscript
            If plhTranscript.Visible Then
                With .Chapters
                    If .Count > 0 Then lblTranscript.Text = HttpUtility.HtmlEncode(String.Concat(.Item(0))).Replace(vbCrLf, "<br/>")
                    'If .Count > 1 Then lblTranscript.Text = HttpUtility.HtmlEncode(String.Concat(.Item(0), vbCrLf, vbCrLf, .Item(1))).Replace(vbCrLf, "<br/>")
                End With
                With .Scribe
                    divScribe.Visible = Not String.IsNullOrEmpty(.ScribeDisplayName.Trim)
                    lnkScribe.Text = .ScribeDisplayName
                    lnkScribe.NavigateUrl = .ScribeDisplayWebsite
                End With
                ctrlTranscript.Display(Product)
                lnkTranscript2.NavigateUrl = CSitemap.TranscriptWithPaging(Product) & "?p=2"
                lnkTranscript3.NavigateUrl = CSitemap.Transcript(Product)

                If .HasTranscriptChineseSimp Then
                    lnkChineseSimp.Visible = True
                    lnkChineseSimp2.Visible = True
                    lnkChineseSimp.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product)
                    lnkChineseSimp2.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product)
                End If
                If .HasTranscriptChineseTrad Then
                    lnkChineseTrad.Visible = True
                    lnkChineseTrad2.Visible = True
                    lnkChineseTrad.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product)
                    lnkChineseTrad2.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product)
                End If
            End If

            'Download links (big buttons)
            If .Files.Mp3s.Count < 2 AndAlso .Files.Htms.Count < 2 Then 'Has 2 files, htm+mp3, or exactly one of each (todo: probably need to revise)
                lnkMp3.Visible = True
                lnkHtm.Visible = True

                With .Files
                    If .HasMp3 Then
                        lnkMp3.NavigateUrl = CConfig.DropboxUrl(.FirstMp3.FileName)
                        lnkMp3.ToolTip = .FirstMp3.FileNameAndSize
                    Else
                        lnkMp3.Enabled = False
                        lnkMp3.CssClass = "disabled"
                        lnkMp3.ToolTip = "Not Available"
                    End If

                    If .HasHtm Then
                        lnkHtm.NavigateUrl = CConfig.DropboxUrl(.FirstHtm.FileName)
                        lnkHtm.ToolTip = .FirstHtm.FileNameAndSize
                    ElseIf .HasPdfOrWord AndAlso Product.IsFree Then
                        lnkHtm.NavigateUrl = CConfig.DropboxUrl(.FirstPdfOrWord.FileName)
                        lnkHtm.ToolTip = .FirstPdfOrWord.FileNameAndSize
                    Else
                        lnkHtm.Enabled = False
                        lnkHtm.CssClass = "disabled"
                        lnkHtm.ToolTip = "Not Available"
                    End If
                End With

                If .HasTranscript Then
                    lnkTranscript.NavigateUrl = CSitemap.TranscriptWithPaging(Product)
                ElseIf .HasVideos Then
                    lnkTranscript.NavigateUrl = .VideoUrl
                    lnkTranscript.Text = "Video"
                Else
                    lnkTranscript.Enabled = False
                    lnkTranscript.CssClass = "disabled"
                    lnkTranscript.ToolTip = "Not Available"
                End If
            Else
                plhButtons.Visible = False
                plh.Visible = True
                Dim p As CProduct = Product
                If p.HasParent Then p = p.BelongsTo
                With p
                    If .Files.HasMobi OrElse .Files.HasEpub Then
                        UCDownload(plh).Display("eBooks")
                        For Each i As CFile In .Files.Epubs
                            UCDownload(plh).Display(i)
                        Next
                        For Each i As CFile In .Files.Mobis
                            UCDownload(plh).Display(i)
                        Next
                        For Each i As CFile In .Files.PdfsOrWords
                            If i.Product.HasParent Then Continue For
                            UCDownload(plh).Display(i) 'e.g. smashwords pdf ebook
                        Next
                        If Not String.IsNullOrEmpty(.ProductAmazonId) Then
                            UCDownload(plh).Display(CSitemap.External_Amazon(.ProductAmazonId), "Kindle via Amazon", " is also available")
                        End If
                    End If

                    If .Files.HasPdfOrWord OrElse .HasBook Then
                        UCDownload(plh).Display("Books")
                        For Each i As CFile In .Files.PdfsOrWords
                            If Not i.Product.HasParent Then Continue For
                            UCDownload(plh).Display(i) 'e.g. Manual
                        Next
                        If .HasBook Then
                            UCDownload(plh).Display(CSitemap.External_CreateSpace(.ProductCSBookIdOrParent), "Printed Book", " is also available (direct)")
                            Dim bb As CProductList = CProduct.Cache.Paperbacks.GetByCSBookId(p.ProductCSBookId)
                            If bb.Count > 0 Then
                                p = bb(0)
                                If Not String.IsNullOrEmpty(p.ProductAmazonId) Then
                                    UCDownload(plh).Display(CSitemap.External_Amazon(.ProductAmazonId), "Printed Book", " is also available (via Amazon)")
                                End If
                            End If
                        End If
                    End If

                    If .Files.HasMp3 Then
                        UCDownload(plh).Display("Audio", "*Save to disk before playing (right-click, save-as)")
                        For Each i As CFile In .Files.Mp3s
                            UCDownload(plh).Display(i)
                        Next
                    End If

                    If .HasVimeoChannel OrElse .Children.HavingOnVimeo.Count > 0 Then
                        UCDownload(plh).Display("Vimeo")
                        If .HasVimeoChannel Then UCDownload(plh).Display(Product, True)
                        For Each i As CProduct In .Children
                            If i.HasVimeo Then UCDownload(plh).Display(i, True)
                        Next
                    End If

                    If .HasYouTubePlaylist OrElse .Children.HavingOnYouTube.Count > 0 Then
                        UCDownload(plh).Display("YouTube")
                        If .HasYouTubePlaylist Then UCDownload(plh).Display(Product, False)
                        For Each i As CProduct In .Children
                            If i.HasYouTube Then UCDownload(plh).Display(i, False)
                        Next
                    End If

                    If .Files.HasHtm AndAlso Not .Files.HasPdfOrWord Then
                        UCDownload(plh).Display("Notes")
                        For Each i As CFile In .Files.Htms
                            UCDownload(plh).Display(i)
                        Next
                    End If
                End With
            End If

        End With
    End Sub
#End Region

#Region "Event Handlers - Form"
#End Region

#Region "User Controls"
    Private Shared Function UCDownload(ByVal target As Control) As UCDownload
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCDownload)
        target.Controls.Add(ctrl)
        Return CType(ctrl, UCDownload)
    End Function
#End Region

End Class

